<?

class admin_util_priv {
	/**
	 * 导入resource
	 *
	 * @param string $path
	 */
	public static function import($path){
		$db = DB::getInstance('admin');
		$files = glob($path."*.php");
		foreach($files as $file){
			$file = realpath($file);
		
			$fileInfo = split('[\/\\]', substr($file, 0, strrpos($file, '.')));
			$fileInfo = array_splice($fileInfo, -4);
			list($app, $_1, $_2, $controller) = $fileInfo;
			$className = implode("_", $fileInfo);
			
			include_once($file);
			$class = new ReflectionClass($className);
			$methods = $class->getMethods();
			foreach($methods as $method){
				$name = $method->getName();
				$regex = '`^([a-z_0-9]+)_action$`i';
				$flag = preg_match($regex, $name, $match);
				if($flag){
					$action = $match[1];
					$methodComment = $method->getDocComment();
					if(preg_match('~@ignore~', $methodComment)){
						continue;
					}
					preg_match('~@name\s(.+)~', $methodComment, $temp);
					
					if(count($temp)!=2){
						$description = "N/A";
					}else{
						$description = $temp[1];
					}
					$params = array(
						'app'=>$app,
						'controller'=>$controller,
						'action'=>$action,
						'description'=>$description
					);
					$db->save('_resources', $params);
				}
			}
			
		}
	}
}